class Item < ActiveRecord::Base
  validates_presence_of :name, :item_style
  validates_uniqueness_of :name
  
  has_many :pictures, :class_name => 'ItemPicture', :dependent => :destroy
  has_one :rating, :class_name  => 'ItemRating', :dependent => :destroy
  has_many :reviews, :class_name => "ItemReview", :dependent => :destroy
  #has_many :short_reviews, :class_name=> 'ItemReview',:dependent => :destroy
  #has_many :hot_reviews, :class_name=> 'ItemReview',:dependent => :destroy
  #has_many :latest_reviews, :class_name=> 'ItemReview',:dependent => :destroy
  #has_many :user_abuse_reports, :as => :abusable
  has_many :user_favorite_infos, :as => :favoritable
  has_many :item_recommends, :as => :recommendable
  has_many :user_item_interests, :dependent => :destroy
  has_many :users, :through => :user_item_interests
  has_many :item_tags, :dependent => :destroy
  has_many :taggings, :through => :item_tags, :source => :tag
  has_many :alliances, :class_name => 'ItemAlliance', :dependent => :destroy
  has_one :statistic, :class_name => 'ItemStatistic', :dependent => :destroy
 
  belongs_to :brand
  belongs_to :item_style, :foreign_key => :style_id
  belongs_to :item_fashion, :class_name => 'ItemStyle', :foreign_key => 'fashion_id'
  belongs_to :cat1, :class_name => 'ItemCategory', :foreign_key => :cat1_id
  belongs_to :cat2, :class_name => 'ItemCategory', :foreign_key => :cat2_id
  
  delegate :name, :description, :to => :item_style, :prefix => true, :allow_nil => true
  delegate :name, :description, :to => :item_fashion, :prefix => true, :allow_nil => true
  
  IMG_SIZE = { :large => '_310x310', :middle => '_120x120', :small => '_60x60', :smaller => '_40x40' }
  
#  define_index do
#    #indexes name, :sortable => true
#    indexes tags, :sortable => true
#    has :name, :created_at, :updated_at, :global_rating, :cat1_id, :cat2_id
#    #    # 声明使用实时索引
#    #    set_property :delta => true
#    #    # 声明使用定时索引
#    #    set_property :delta => :datetime, :threshold => 1.hour
#    #    # 声明使用延时索引
#    #    set_property :delta => :delayed
#  end
  
  { :hot => 1, :top => 2, :recommend => 3, :hot_comment => 4, :recent_comment => 5 }.each do |method_name, flag|
    scope method_name, :conditions => { :flag => flag }
  end
  
  scope :picture_count, lambda {|item_id| {:select=>"count(id) as vul",:from=>:item_pictures,:conditions=>["item_id=?",item_id]}}
  
  scope :review_count, lambda {|item_id,flag| {:select=>"count(id) as vul",:from=>:item_reviews,:conditions=>["item_id=? and flag=?",item_id,flag]}}
  
  scope :related_user, lambda {|item_id,limit| {:select=>'r.user_id,r.item_id,r.tags,u.nick_name,u.user_name,u.user_pic,u.fans_total',:from=>:item_ratings,:joins=>' r join users u on r.user_id=u.id',:limit=>limit,:conditions=>['item_id=?',item_id]}}
  
  def to_s
    name
  end
  
  #  def to_param
  #    "#{id}-#{name}"
  #  end

  #主图片
  def mp310
    self.main_pic.blank? ? "" : self.main_pic + "_310x310.jpg"
  end
  
  def img(size = :middle)
    main_pic.blank? ? "" : "#{main_pic}#{IMG_SIZE[size.to_sym]}.jpg"
  end

  def cat_recommends
    a = [self.cat1_id,self.cat2_id]
    a.delete(nil)
    Item.all(:limit=>9,:conditions=>["id != ? and (cat1_id is not null and cat1_id in (?)) or (cat2_id is not null and cat2_id in (?))",self.id,a,a])
  end

  #第一点评的用户
  def first_review_user
    self.reviews.first(:order => "created_at desc").try(:user)
  end

  def hot_reviews(limit=nil)
    self.reviews.find(:all,:limit => limit,:order => "reply_count desc" )
  end

  def latest_reviews(limit=nil)
    self.reviews.find(:all,:limit => limit,:order => "created_at desc" )
  end

  def user_rating(user)
    return 0 if user.blank?
    ItemRating.find_by_item_id_and_user_id(self.id,user.id)
  end

  def bought_users(limit=nil)
    self.users.all(:include => :user_item_interests,:limit=>limit,:conditions=>["user_item_interests.interest = 1"])
  end

  def want_buy_users(limit=nil)
    self.users.all(:include => :user_item_interests,:limit=>limit,:conditions=>["user_item_interests.interest = 0"])
  end

  def same_cat_items(limit=nil)
    a = [self.cat1_id,self.cat2_id]
    a.uniq!
    Item.all(:limit=>limit,:conditions=>["id != ? and (cat1_id in (?) or cat2_id in (?))",self.id,a,a])
  end

end
